#!/usr/bin/env python

from address_book_class import AddressBook
import sys


#{{{def _create_dataset ( start_point, lines, rows, tot_rows ):
def _create_dataset ( start_point, lines, rows, tot_rows ):
	res = {}

	count = 0
	for v in rows:
		v [ "_pos" ] = start_point + count
		res [ "row%d" % count ] = v
		count += 1

	res [ 'rows' ] = tot_rows
	res [ 'from_row' ] = start_point
	res [ 'to_row' ] = start_point + count
	res [ 'lines' ] = lines

	return res
#}}}

def get_info ( liwe, address = 0 ):
	"""
        :INPUT:
	"""
	uid = liwe.pyhp.session.data.get ( "uid", -1 )
	email = liwe.pyhp.session.data.get ( "email", "" )

	ab = AddressBook ( liwe )
	if uid == -1:
		liwe.set_error ( '1', ab.templates [ 'SITE_NOLOGIN_MSG' ] )
		return 
	resp =  ab.get_profile ( uid = uid )
	#if not resp: resp = {} 
	if not resp:
		liwe.set_error ( '2', ab.templates [ 'SITE_NOPROFILE_MSG' ] )
		return  { 'uid' : uid, 'email' : email } 
	resp [ 'uid' ] = uid
	resp [ 'email' ] = email
	if address: resp [ 'address' ] = ab.list_address ( id_profile = resp.get ( 'id_profile' , 0 ) )
	return { 'profile' : resp }
		

#{{{def get_profile ( liwe, uid = 0 ):
def get_profile ( liwe, id_profile = 0, uid = 0 ):
	"""
	:INPUT:
		- id_profile:	SAFE_NUMBER
		- uid:			SAFE_NUMBER
	"""
	ab = AddressBook ( liwe )
	resp =  ab.get_profile ( id_profile =  id_profile, uid = uid ) 
	return { 'profile' : resp }
#}}}	
def get_templates ( liwe ):
	"""
        :INPUT:
	"""
	ab = AddressBook ( liwe )
	return { "templates" : ab.get_templates () }

def list_regioni ( liwe ):
	ab = AddressBook ( liwe )
	return { 'regioni' : ab.list_regioni () }

def list_province ( liwe, regione = '' ):
	ab = AddressBook ( liwe )
	return { 'province' : ab.list_province ( regione = regione ) }
def list_stati ( liwe ):
	ab = AddressBook ( liwe )
	return { 'stati' : ab.list_stati () }

def get_categ ( liwe, id_categ):
	"""
	:INPUT:
		- id_categ:              SAFE_NUMBER
	"""

	ab = AddressBook ( liwe )
	return { 'categ': ab.get_categ ( id_categ ) }

#{{{def set_profile ( liwe, uid = 0, name = "", lastname = "",email = "", phone = "", fax ="", level = 0, discount = "", tax_code ="" , vat_code = "", logo = "", note = "", owner = 0 ):
def set_profile ( liwe, id_profile = 0, uid = 0, name = "", lastname = "",email = None, phone = None, cell = None, fax =None, level = None, 
			  discount = None, tax_code = None, vat_code = None, addr1 = None, zip_code = None, city  = None, prov = None,
			  country = None, logo = None, note = None, owner = 0, id_categ = None, company = None, individual = None, ):
	"""     
	:INPUT:         
		- id_profile:	SAFE_NUMBER       
		- uid: 			SAFE_NUMBER       
		- name:			SAFE_STRING	
		- lastname:		SAFE_DB_STRING	
		- email:		SAFE_DB_STRING	
		- phone:		SAFE_DB_STRING	
		- cell:			SAFE_DB_STRING	
		- fax:			SAFE_DB_STRING	
		- level:		SAFE_NUMBER	
		- discount:		SAFE_DB_STRING	
		- tax_code:		SAFE_DB_STRING	
		- vat_code:		SAFE_DB_STRING	
		- addr1:		SAFE_DB_STRING
		- zip_code:		SAFE_DB_STRING
		- city:			SAFE_DB_STRING
		- prov:			SAFE_DB_STRING	
		- country:		SAFE_DB_STRING	
		- logo: 		SAFE_DB_STRING
		- note:			SAFE_HTML
		- owner:		SAFE_NUMBER		
		- id_categ:		SAFE_NUMBER       
		- company:		SAFE_NUMBER
		- individual:	SAFE_NUMBER
	"""

	ab = AddressBook ( liwe )

	#Get user in session
	#FIXME: ROOT NON PUO MODIFICARE IL SUO PROFILO NEL FRONT END	
	userid = liwe.pyhp.session.data.get ( "uid", -1 )
	if userid != 1 and userid != -1:
		perms =  liwe.invoke ( 'system.ajax.module_get_perms', 'address_book'  )
		if not 'create' in perms.get( 'perms', [] ):
			uid = userid
			#do not lost fileds setting from admin
			profile = ab.get_profile ( uid = uid )
			if not profile: profile = {}
			fields = [ 'id_profile', 'company', 'individual', 'id_category', 'level', 'email', 'note' ]
			id_profile = profile.get ( 'id_profile' , 0 )
			id_category = profile.get ( 'id_category' , 0 )
			level = profile.get ( 'level' , 0 )
			note = profile.get ( 'note' , '' )
			discount = profile.get ( 'discount' , 0 )



	resp = ab.set_profile ( id_profile = id_profile, uid = uid, name = name, lastname = lastname,email = email, phone = phone, cell =  cell,
				fax= fax, level = level, discount = discount, tax_code = tax_code , vat_code = vat_code, addr1 = addr1, 
				zip_code = zip_code, city = city, prov = prov, country = country, logo = logo, note = note, owner = owner, 
				id_categ = id_categ, company = company, individual = individual )
	return { 'profile' : resp }

#}}}

#{{{def set_address ( liwe, id_address = 0, id_profile = 0, id_addr_kind = 0, addr1 = "", addr2 = "", zip_code = "", city  = "", prov = "", country = "", phone = "", mail = "", owner = 0 ):
def set_address ( liwe, id_address = 0, id_profile = 0, id_addr_kind = 0, addr1 = "", addr2 = "", zip_code = "", city  = "", prov = "", country = "", phone = "", mail = "", owner = 0 ):
	"""
	:INPUT:
		- id_address:	SAFE_NUMBER
		- id_profile:	SAFE_NUMBER
		- id_addr_kind:	SAFE_NUMBER
		- addr1:		SAFE_DB_STRING
		- addr2:		SAFE_DB_STRING
		- zip_code:		SAFE_DB_STRING
		- city:			SAFE_DB_STRING
		- prov:			SAFE_DB_STRING	
		- country:		SAFE_DB_STRING	
		- phone:		SAFE_DB_STRING
		- mail:			SAFE_DB_STRING
		- owner:		SAFE_NUMBER
	"""
	ab = AddressBook ( liwe )
	resp =  ab.set_address ( id_address = id_address, id_profile = id_profile, id_addr_kind = id_addr_kind, addr1 = addr1, addr2 = addr2, zip_code = zip_code, 
				city = city, prov = prov, country = country, phone = phone, mail = mail, owner =  owner )
	
	return { 'address' : resp }	
#}}}
#{{{def get_address ( liwe, id_address = 0, uid = 0 ):
def get_address ( liwe, id_address = 0, id_profile = 0, get_default = 0 ):
	"""
	:INPUT:
		-id_address:	SAFE_NUMBER
		-id_profile:	SAFE_NUMBER
		-get_default:	SAFE_NUMBER
	"""
	ab = AddressBook ( liwe )
	resp = ab.get_address ( id_address = id_address, id_profile = id_profile, get_default = get_default )
	return { 'address' : resp }
#}}}		
#{{{def list_address ( liwe, addr1 = '', addr2 = '', zip_code = '', city = '', prov = '', country = '', phone = '',quant = 0, _X_LINES = -1, _X_START_POINT = 0, _X_PAGE = 0 ):
def list_address ( liwe, id_profile = 0, addr1 = '', addr2 = '', zip_code = '', city = '', prov = '', country = '', phone = '',quant = 0, _X_LINES = -1, _X_START_POINT = 0, _X_PAGE = 0 ):
	"""
	:INPUT:
		- id_profile:		SAFE_NUMBER
		- addr1:			SAFE_DB_STRING
		- addr2:			SAFE_DB_STRING
		- zip_code:			SAFE_DB_STRING
		- city:				SAFE_DB_STRING
		- prov:				SAFE_DB_STRING
		- coutry:			SAFE_DB_STRING
		- phone:			SAFE_DB_STRING
		- quant:			SAFE_NUMBER
		- _X_LINES:			SAFE_NUMBER
		- _X_START_POINT:	SAFE_NUMBER
		- _X_PAGE:			SAFE_NUMBER
	"""
	use_dataset = 0

	if _X_LINES != -1: 
		quant = _X_LINES
		use_dataset = 1

	if _X_START_POINT:
		start_point = _X_START_POINT
	else:
		start_point = _X_PAGE * quant

	ab = AddressBook ( liwe )
	resp = ab.list_address ( id_profile = id_profile, addr1 = addr1, addr2 = addr2, zip_code = zip_code, city = city, prov = prov,
				country = country, phone = phone, start_point = start_point, do_count = use_dataset )

	rows = resp [ 'rows' ]
	tot_rows = resp [ 'tot_rows' ]

	if not use_dataset: 
		return { 'address': rows }

	return _create_dataset ( start_point, quant, rows, tot_rows )
#}}}


def address_select ( liwe ):
	ab = AddressBook ( liwe )
	return  ab.address_select()


#{{{def list_kind ( liwe, descr = '', quant = 0, _X_LINES = -1, _X_START_POINT = 0, _X_PAGE = 0 ):
def list_kind ( liwe, descr = '', quant = 0, _X_LINES = -1, _X_START_POINT = 0, _X_PAGE = 0 ):
	"""
	:INPUT:
		- descr:		SAFE_DB_STRING
		- quant:		SAFE_NUMBER
		- _X_LINES:		SAFE_NUMBER
		- _X_START_POINT:	SAFE_NUMBER
		- _X_PAGE:		SAFE_NUMBER
	"""

	use_dataset = 0

	if _X_LINES != -1: 
		quant = _X_LINES
		use_dataset = 1

	if _X_START_POINT:
		start_point = _X_START_POINT
	else:
		start_point = _X_PAGE * quant

	ab = AddressBook ( liwe )
	resp = ab.list_kind ( descr = descr, start_point = start_point, do_count = use_dataset )

	rows = resp [ 'rows' ]
	tot_rows = resp [ 'tot_rows' ]

	if not use_dataset: 
		return { 'kind': rows }

	return _create_dataset ( start_point, quant, rows, tot_rows )
#}}}
def comune_search ( liwe, prov ):
	"""
	:INPUT:
		- prov: SAFE_STRING
	""" 
	ab = AddressBook ( liwe )
	return { "comu_list" : ab.comune_search ( prov ) }


def cap_search ( liwe, prov, city ):
	"""
	:INPUT:
		- prov: SAFE_STRING
		- city: SAFE_STRING
	""" 
	ab = AddressBook ( liwe )

	return { "cap_list" : ab.cap_search ( prov, city) }

def list_categs ( liwe, id_node = None ):
	"""
	:INPUT:
		- id_news:              SAFE_NUMBER
		- id_node:              SAFE_NUMBER
	"""

	ab = AddressBook ( liwe )

	rs = ab.list_categs ( id_node )
	if not rs: rs = []

	return { 'categs': rs }
